<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组排序</title>
</head>
<body>
    <script>
        let arr = [435,65,254,8768,9898];
        // arr = arr.sort(function (a,b) {
        //     // [1,2] a-b  -1 从小到大，b-a 1 从大到小
        //     return a - b;
        // });
        // console.log(arr);


        let lessons = [
            {lesson:"chinese",fs:56},
            {lesson:"math",fs:88},
            {lesson:"english",fs:78}
        ];
        lessons = lessons.sort(function (a,b) {
            return a.fs - b.fs;
        });
        console.log(lessons);




        // sort方法的实现原理
        function sorts(array,callback) {
            for (const m in array) {
                for (const n in array) {
                    if (callback(array[m],array[n]) < 0) {
                        let temp = array[m];
                        array[m] = array[n];
                        array[n] = temp;                  
                    }
                };
            };
            return array;
        };

        arr = sorts(arr,function (a,b) {
            return a - b;
        })
        console.log(arr);

    </script>
</body>
</html>